package com.hycreate.platform.modules.app.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.security.annotation.RequiresUser;
import com.hycreate.platform.common.utils.mypage.PageDemo;
import com.hycreate.platform.common.utils.mypage.PageDemoUtil;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.app.entity.BaseData;
import com.hycreate.platform.modules.app.entity.BreadcrumbNavigation;
import com.hycreate.platform.modules.app.entity.ConfigData;
import com.hycreate.platform.modules.app.entity.ResetData;
import com.hycreate.platform.modules.atta.SfAttachment;
import com.hycreate.platform.modules.atta.SfAttachmentService;
import com.hycreate.platform.modules.config.SfConfig;
import com.hycreate.platform.modules.config.SfConfigService;
import com.hycreate.platform.modules.element.SfElement;
import com.hycreate.platform.modules.element.SfElementService;
import com.hycreate.platform.modules.product.SfProduct;
import com.hycreate.platform.modules.product.SfProductService;
import com.hycreate.platform.modules.product.element.SfProductElement;
import com.hycreate.platform.modules.product.element.SfProductElementService;
import com.hycreate.platform.modules.project.SfProject;
import com.hycreate.platform.modules.project.SfProjectService;
import com.hycreate.platform.modules.project.product.SfProjectProduct;
import com.hycreate.platform.modules.project.product.SfProjectProductService;
import com.hycreate.platform.modules.service.SfService;
import com.hycreate.platform.modules.service.SfServiceService;
import com.hycreate.platform.modules.voice.SfVoice;
import com.hycreate.platform.modules.voice.SfVoiceService;

@Controller
@RequestMapping("baseData")
@RequiresUser(required = false)
// 关闭登录验证，必须要加上
public class BaseDataController extends BaseController {

	@Autowired
	private SfProjectService sfProjectService;

	@Autowired
	private SfProductService sfProductService;

	@Autowired
	private SfElementService sfElementService;

	@Autowired
	private SfProjectProductService sfProjectProductService;

	@Autowired
	private SfProductElementService sfProductElementService;

	@Autowired
	private SfAttachmentService sfAttachmentService;

	@Autowired
	private SfServiceService sfServiceService;

	@Autowired
	private SfConfigService sfConfigService;

	@Autowired
	private SfVoiceService sfVoiceService;

	@RequestMapping("allData")
	@ResponseBody
	public BaseData getAllData() {
		BaseData bd = new BaseData();

		List<SfProject> pList = sfProjectService.findList(null);

		List<SfProduct> productList = sfProductService.findList(null);

		List<SfElement> elmentList = sfElementService.findList(null);

		List<SfProjectProduct> ppList = sfProjectProductService.findList(null);

		List<SfProductElement> peList = sfProductElementService.findList(null);

		List<SfAttachment> attachmentList = sfAttachmentService.findList(null);

		bd.setCode("200");
		bd.setMsg("请求成功");
		bd.setProject(pList);
		bd.setProduct(productList);
		bd.setElement(elmentList);
		bd.setProjectProduct(ppList);
		bd.setProductElement(peList);
		bd.setAttachment(attachmentList);

		System.out.println(bd.toString());

		return bd;
	}

	@ResponseBody
	@RequestMapping("getService")
	public List<SfService> getService() {
		return sfServiceService.findList(null);
	}

	@RequestMapping("configData")
	@ResponseBody
	public ConfigData getConfigData() {
		ConfigData cd = new ConfigData();

		cd.setCode("200");
		cd.setMsg("请求成功");

		SfConfig sfConfig = sfConfigService
				.get("07beea1f8853405bb2aff16726b9c00d");
		sfConfig.setStatus("2");
		sfConfigService.save(sfConfig);

		SfConfig sfConfigCopy = new SfConfig();
		sfConfigCopy.setCsLanguage(sfConfig.getCsLanguage());
		sfConfigCopy.setCsShow(sfConfig.getCsShow());
		sfConfigCopy.setCsShowImgs(sfConfig.getCsShowImgs());

		cd.setSfConfig(sfConfigCopy);

		return cd;
	}

	@RequestMapping("reset")
	@ResponseBody
	public ResetData reset() {
		ResetData rd = new ResetData();

		SfConfig sfConfig = sfConfigService
				.get("07beea1f8853405bb2aff16726b9c00d");

		rd.setCode("200");
		rd.setMsg("请求成功");

		rd.setStatus(sfConfig.getStatus());

		return rd;
	}

	/**
	 * 获取语音广播
	 * 
	 * @return
	 */
	@RequestMapping("voice")
	@ResponseBody
	public SfVoice voide() {
		SfVoice sfVoice = null;
		List<SfVoice> list = sfVoiceService.findList(new SfVoice());
		if (CollectionUtils.isNotEmpty(list)) {
			sfVoice = list.get(0);
			String status = sfVoice.getStatus();
			if (sfVoice != null) {
				sfVoice.setStatus("2");
				sfVoiceService.save(sfVoice);
				sfVoice.setStatus(status);
			}
		}

		return sfVoice;
	}

	/**
	 * 新的界面的首页
	 * 
	 * @param request
	 * @param redirectAttributes
	 * @return
	 */

	@RequestMapping(value = "newinterface")
	public String newinterface(HttpServletRequest request,
			RedirectAttributes redirectAttributes) {

		return "modules/project/newinterface/index";
	}

	/**
	 * 新的界面 跳转到项目列表界面
	 * 
	 * @param sfProject
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */

	@RequestMapping(value = "projectlist")
	public String projectlist(SfProject sfProject, HttpServletRequest request,
			HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes) throws ServletException,
			IOException {

		 int everyPage=30; //每页30条

		 int current=0;

		/* 前台传到数据 */

		 String type= request.getParameter("type");
		
		String str = request.getParameter("search");

		sfProject.setName(str);

		 int total= sfProjectService.projectCount(sfProject);
		 int totalPages=PageDemoUtil.Comparison(total,everyPage); //总页数

		 PageDemo pd=PageDemoUtil.Calculation(total, current, type,everyPage);

		 pd.setValueId(str);
	   List<SfProject> list=sfProjectService.projectListPages(pd);
	   
	  
	   
		model.addAttribute("list", list);

		model.addAttribute("search", str);
		
		model.addAttribute("tp", totalPages);
		
		model.addAttribute("total", total);
		

		return "modules/project/newinterface/projectList";

	}

	/**
	 * 项目列表页面的ajax跳转
	 * @param sfProject
	 * @param valStr
	 * @param type
	 * @param currentStr
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "ajaxProject")
	public Map<String, Object> ajaxProject(SfProject sfProject, String valStr,String type, String currentStr, HttpServletRequest request,
			HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes) {

		 int everyPage=30; //每页30条

			// 转换前台的当前页
		int current = Integer.parseInt(currentStr);


		sfProject.setName(valStr);

		 int total= sfProjectService.projectCount(sfProject);
		 int totalPages=PageDemoUtil.Comparison(total,everyPage); //总页数

		 PageDemo pd=PageDemoUtil.Calculation(total, current, type,everyPage);

		 pd.setValueId(valStr);
		
		
	   List<SfProject> list=sfProjectService.projectListPages(pd);
	   
	   
	   Map<String, Object> jsonMap = new HashMap<String, Object>();


		jsonMap.put("list", list);

		jsonMap.put("tp", totalPages);
		
		jsonMap.put("current", pd.getCurrentPage());
		
		
		return jsonMap;

	}
	
	/**
	 * 跳转到项目表单界面
	 * 
	 * @param sfProject
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */

	@RequestMapping(value = "projectform")
	public String projectform(SfProject sfProject, HttpServletRequest request,
			HttpServletResponse response, Model model) {

		/* 前台传来数据 */
		String id = request.getParameter("id");
		// 查找这个id所属的那个项目对象
		sfProject = sfProjectService.get(id);
		// 如果你的对象不为空就查找它的图片
		if (sfProject != null) {

			List<SfAttachment> SA = sfProjectService.findProjctPic(sfProject
					.getId());

			for (SfAttachment sfAttachment : SA) {
				if (sfAttachment != null) {

					sfProject.setProjectPicUrl(SA.get(0).getUrl());
				}

			}
		}

		// 根据这个传过来的项目id查找这个项目下面的产品集合
		List<SfProduct> list = sfProjectService.findProductPage(id);
		// 把集合分解开，方便调用
		 for (int i = 0; i < list.size(); i++) {
			if (list.get(i) != null) {
				// 在list里的对象部位不为null的时候，用对象的id查找出与产品对象相关联的SfAttachment对象集合
				List<SfAttachment> SA = sfProductService
						.findProductPic(list.get(i).getId());

				for (SfAttachment sfAttachment : SA) {

					if (sfAttachment != null) {
						// 把SfAttachment对象集合的第一个对象的url属性添加到产品的图像属性上
						list.get(i).setProductPicURL(SA.get(0).getUrl());
					}

				}

			}else{
				
				  list.remove(i);
			}
			  
		}
		 
		  if(list.isEmpty()){
			  
			  return "modules/project/newinterface/productUtil";
		  }
		
		
		model.addAttribute("sfProject", sfProject);
		model.addAttribute("list", list);
		return "modules/project/newinterface/projectForm";
	}

	/**
	 * 产品列表页面
	 * 
	 * @param sfProduct
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */

	@RequestMapping(value = "productlist")
	public String productlist(SfProduct sfProduct, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		 int everyPage=30; //每页30条

		 int current=0;

		/* 前台传到数据 */

		 String type= request.getParameter("type");
		
		 String str = request.getParameter("search");
		
		 sfProduct.setCode(str);
		
		 int total= sfProductService.productCount(sfProduct);
		 int totalPages=PageDemoUtil.Comparison(total,everyPage); //总页数

		 PageDemo pd=PageDemoUtil.Calculation(total, current, type,everyPage);

		 pd.setValueId(str);
		
		 List<SfProduct> list=sfProductService.findPageList(pd);
		 
		 
		model.addAttribute("list", list);
		 
		model.addAttribute("search", str);
		
		model.addAttribute("tp", totalPages);
		
		model.addAttribute("total", total);

		return "modules/project/newinterface/productList";
	}

	
	@ResponseBody
	@RequestMapping(value = "ajaxProduct")
	public Map<String, Object> ajaxProduct(SfProduct sfProduct, String valStr,String type, String currentStr, HttpServletRequest request,
			HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes) {
		
		
		 int everyPage=30; //每页30条

			// 转换前台的当前页
		int current = Integer.parseInt(currentStr);

		String str = request.getParameter("search");

		  sfProduct.setCode(str);
		 int total= sfProductService.productCount(sfProduct);
		 
		 int totalPages=PageDemoUtil.Comparison(total,everyPage); //总页数

		 PageDemo pd=PageDemoUtil.Calculation(total, current, type,everyPage);

		 pd.setValueId(str);
		 
	     List<SfProduct> list=sfProductService.findPageList(pd);
		
	     System.out.println("-------------------"+list);
	     

	     
		
		 Map<String, Object> jsonMap = new HashMap<String, Object>();
		 
		 
		   jsonMap.put("list", list);

			jsonMap.put("tp", totalPages);
			
			jsonMap.put("current", pd.getCurrentPage());
		
			
		
		return jsonMap;
	}
	
	/**
	 * 产品表单页面：待定界面，初期是为了界面的初始化操作，要重构
	 * 
	 * @param sfProject
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "productform")
	public String productform(SfProduct sfProduct, SfElement sfElement,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		int everyPage = 4;

		String id = request.getParameter("id");

		String pid = request.getParameter("pid");

		sfProduct = sfProductService.get(id);

		sfProduct.setProjectId(id);

		int total = sfProductService.elementCount(id); // 配件总数

		int totalPages = PageDemoUtil.Comparison(total, everyPage); // 总页数

		PageDemo pd = new PageDemo();
		pd.setValueId(id);
		pd.setBeginIndex(0);
		pd.setEndinIndex(4);
		int current = 1;

		if (sfProduct != null) {

			List<SfAttachment> SA = sfProductService.findProductPic(sfProduct
					.getId());

			for (SfAttachment sfAttachment : SA) {

				if (sfAttachment != null) {
					// 把SfAttachment对象集合的第一个对象的url属性添加到产品的图像属性上
					sfProduct.setProductPicURL(SA.get(0).getUrl());
				}

			}
		}

		List<SfElement> list = sfProductService.findElement(pd);
		if(!list.isEmpty()){
			
		
		
		for (SfElement Element : list) {
			if (Element != null) {
				List<SfAttachment> SA = sfElementService.findElementPic(Element
						.getId());
				for (SfAttachment sfAttachment : SA) {
					if (sfAttachment != null) {

						Element.setElemenetPicURL(SA.get(0).getUrl());

					}

				}

			} else {
				return "modules/project/newinterface/productUtil";
			}
		}
		}else{
			
			return "modules/project/newinterface/productUtil";
		}
		
		model.addAttribute("sfProduct", sfProduct);

		model.addAttribute("list", list);

		model.addAttribute("current", current);

		model.addAttribute("tp", totalPages);

		model.addAttribute("pid", pid);
		
		model.addAttribute("total", total);

		return "modules/project/newinterface/productForm";
	}

	@ResponseBody
	@RequestMapping(value = "pageform")
	public Map<String, Object> pageform(SfProduct sfProduct, String productId,
			String type, String currentStr, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		//每页分4条
		int everyPage = 4;
		
		
		// 转换前台的当前页
		int current = Integer.parseInt(currentStr);
		
		
		// 通过id获得产品对象
		sfProduct = sfProductService.get(productId);
		// 根据id查出这个产品的配件的数量
		int total = sfProductService.elementCount(productId);

		int totalPages = PageDemoUtil.Comparison(total, everyPage); // 总页数
		

		// 根据总数量和当前页，得出需要的分页的开始下标
		PageDemo pd = PageDemoUtil.Calculation(total, current, type, everyPage);
		
		
		
		// 追加查询的条件
		pd.setValueId(productId);

		// 当对象不为空的时候，把产品的图片地址提取出来
		if (sfProduct != null) {

			
			List<SfAttachment> SA = sfProductService.findProductPic(sfProduct
					.getId());

			for (SfAttachment sfAttachment : SA) {

				if (sfAttachment != null) {
					// 把SfAttachment对象集合的第一个对象的url属性添加到产品的图像属性上
					sfProduct.setProductPicURL(SA.get(0).getUrl());
				}

			}
		}
		// 通过方法查询所要的列表数据
		List<SfElement> elist = sfProductService.findElement(pd);
		
		for (SfElement sfElement : elist) {
			
			if(sfElement!=null){
				  List<SfAttachment> SA = sfElementService.findElementPic(sfElement.getId());
				   for (SfAttachment sfAttachment : SA) {
					if (sfAttachment != null) {
						sfElement.setElemenetPicURL(SA.get(0).getUrl());
						
					}
					
				   }
			}else{
				
				continue;
			}
			
		}
		//用map 和 @ResponseBody这个注解把json传到 前台
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		
		jsonMap.put("list", elist);
		
		jsonMap.put("tp", totalPages);
		
		jsonMap.put("current",pd.getCurrentPage() );
		
		
		
		return jsonMap;
	}

	@ModelAttribute
	public void test(HttpServletRequest request, HttpSession session,
			@RequestParam(value = "goback", required = false) Integer goback) {
		/* 从sssion中获取导航集合 */
		List<BreadcrumbNavigation> bnas = (List<BreadcrumbNavigation>) session
				.getAttribute("bnas");
		/* 如果goback不存在或者为0，则说明需要继续追加导航 */
		if (goback == null || goback == 0) {
			/* 获取请求的地址 */
			String requestUrl = ".." + request.getServletPath();
			/* 如果请求地址为首页地址，则清除导航 */
			if (requestUrl.equals("../baseData/newinterface")) {
				session.removeAttribute("bnas");
				bnas = new ArrayList<BreadcrumbNavigation>();
			}
			/* 如果集合不存在，则新建 */
			if (bnas == null) {
				bnas = new ArrayList<BreadcrumbNavigation>();
			}
			String label = "";
			/* 分析求情地址 */
			switch (requestUrl) {
			case "../baseData/newinterface":
				label = "首页";
				break;
			case "../baseData/productlist":
				label = "产品列表";
				break;
			case "../baseData/productform":
				label = "产品配件展示";
				break;
			case "../baseData/projectlist":
				label = "项目列表";
				break;
			case "../baseData/projectform":
				label = "项目详情";
				break;
			case "../baseData/enInterFace":	
				label = "Home";
				break;
			case "../baseData/enProjectList":	
				label = "Project List";
				break;
			case "../baseData/enProjectForm":	
				label = "Project From";
				break;
			case "../baseData/enProductList":	
				label = "Product List";
				break;
			case "../baseData/enProductForm":	
				label = "Product From";
				break;	
				
			default:
				return;
			}
			/* 获取所有请求参数 */
			Map<String, String[]> parameteMaps = request.getParameterMap();

			int count = 0;
			/* 拼接URL */
			for (Entry<String, String[]> entry : parameteMaps.entrySet()) {
				count++;
				if (!entry.getKey().equals("goback")) {
					if (count == 1) {
						requestUrl += "?" + entry.getKey() + "="
								+ entry.getValue()[0];
					} else {
						requestUrl += "&" + entry.getKey() + "="
								+ entry.getValue()[0];
					}
				}
			}
			/* 创建新的导航对象并赋值 */
			BreadcrumbNavigation bna = new BreadcrumbNavigation();
			bna.setLabel(label);
			bna.setUrl(requestUrl);
			/* 如果集合中最后一个导航与要添加的导航页面相同，说明是刷新或者分页操作，覆盖旧的数据 */
			if (bnas.size() >= 1
					&& bnas.get(bnas.size() - 1).getLabel()
							.equals(bna.getLabel())) {
				bnas.remove(bnas.size() - 1);
			}
			/* 将导航对象放入到集合中 */
			bnas.add(bna);
			session.removeAttribute("bnas");
			session.setAttribute("bnas", bnas);
		} else {
			/* 如果goback参数存在，则说明是往回跳转，则需要删除多余的导航数据 */
			for (int i = 0; i < goback - 1; i++) {
				bnas.remove(bnas.size() - 1);
			}
			/* 重新设置导航集合 */
			session.removeAttribute("bnas");
			session.setAttribute("bnas", bnas);
		}
	}

	// 返回的页数的方法
	private Model Paging(Model model, int current, int total, String type,
			int everyPage) {
		if (type.equals("hasPrePage")) {

			if (current > 1) {
				return model.addAttribute("current", current);
			} else {
				return model.addAttribute("current", 1);
			}

		} else if (type.equals("hasNextPage")) {
			if (current < PageDemoUtil.Comparison(total, everyPage)) {
				return model.addAttribute("current", current);
			} else {
				return model.addAttribute("current",
						PageDemoUtil.Comparison(total, everyPage));
			}
		} else if (type.equals("startPage")) {
			return model.addAttribute("current", 0);
		} else if (type.equals("endPage")) {
			return model.addAttribute("current",
					PageDemoUtil.Comparison(total, everyPage));
		} else if (type.equals("pagenumb")) {

			return model.addAttribute("current", current);
		} else {

			return model;
		}

	}

	
	
	/** 英文版 界   **/
	
	
	@RequestMapping(value = "enInterFace")
	public String enInterface(HttpServletRequest request,
			RedirectAttributes redirectAttributes) {

		return "modules/project/eninterface/index";
	}
	
	
	
	//enProjectList
	
	@RequestMapping(value = "enProjectList")
	public String enProjectList(SfProject sfProject, HttpServletRequest request,
			HttpServletResponse response, Model model,
			RedirectAttributes redirectAttributes) throws ServletException,
			IOException {

		 int everyPage=30; //每页30条

		 int current=0;

		/* 前台传到数据 */

		 String type= request.getParameter("type");
		
		String str = request.getParameter("search");

		sfProject.setName(str);

		 int total= sfProjectService.projectCount(sfProject);
		 int totalPages=PageDemoUtil.Comparison(total,everyPage); //总页数

		 PageDemo pd=PageDemoUtil.Calculation(total, current, type,everyPage);

		 pd.setValueId(str);
	   List<SfProject> list=sfProjectService.projectListPages(pd);
	   
	  
	   
		model.addAttribute("list", list);

		model.addAttribute("search", str);
		
		model.addAttribute("tp", totalPages);
		
		model.addAttribute("total", total);
		

		return "modules/project/eninterface/projectList";

	}
	
	
	
	
	@RequestMapping(value = "enProjectForm")
	public String enProjectForm(SfProject sfProject, HttpServletRequest request,
			HttpServletResponse response, Model model) {

		/* 前台传来数据 */
		String id = request.getParameter("id");
		// 查找这个id所属的那个项目对象
		sfProject = sfProjectService.get(id);
		// 如果你的对象不为空就查找它的图片
		if (sfProject != null) {

			List<SfAttachment> SA = sfProjectService.findProjctPic(sfProject
					.getId());

			for (SfAttachment sfAttachment : SA) {
				if (sfAttachment != null) {

					sfProject.setProjectPicUrl(SA.get(0).getUrl());
				}

			}
		}

		// 根据这个传过来的项目id查找这个项目下面的产品集合
		List<SfProduct> list = sfProjectService.findProductPage(id);
		// 把集合分解开，方便调用
		 for (int i = 0; i < list.size(); i++) {
			if (list.get(i) != null) {
				// 在list里的对象部位不为null的时候，用对象的id查找出与产品对象相关联的SfAttachment对象集合
				List<SfAttachment> SA = sfProductService
						.findProductPic(list.get(i).getId());

				for (SfAttachment sfAttachment : SA) {

					if (sfAttachment != null) {
						// 把SfAttachment对象集合的第一个对象的url属性添加到产品的图像属性上
						list.get(i).setProductPicURL(SA.get(0).getUrl());
					}

				}

			}else{
				
				  list.remove(i);
			}
			  
		}
		 
		  if(list.isEmpty()){
			  
			  return "modules/project/newinterface/productUtil";
		  }
		
		
		model.addAttribute("sfProject", sfProject);
		model.addAttribute("list", list);
		return "modules/project/eninterface/projectForm";
	}
	
	
	//enProductList
	
	
	@RequestMapping(value = "enProductList")
	public String enProductList(SfProduct sfProduct, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		 int everyPage=30; //每页30条

		 int current=0;

		/* 前台传到数据 */

		 String type= request.getParameter("type");
		
		 String str = request.getParameter("search");
		
		 sfProduct.setCode(str);
		
		 int total= sfProductService.productCount(sfProduct);
		 int totalPages=PageDemoUtil.Comparison(total,everyPage); //总页数

		 PageDemo pd=PageDemoUtil.Calculation(total, current, type,everyPage);

		 pd.setValueId(str);
		
		 List<SfProduct> list=sfProductService.findPageList(pd);
		 
		 
		model.addAttribute("list", list);
		 
		model.addAttribute("search", str);
		
		model.addAttribute("tp", totalPages);
		
		model.addAttribute("total", total);

		return "modules/project/eninterface/productList";
	}

	
	@RequestMapping(value = "enProductForm")
	public String enProductForm(SfProduct sfProduct, SfElement sfElement,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		int everyPage = 4;

		String id = request.getParameter("id");

		String pid = request.getParameter("pid");

		sfProduct = sfProductService.get(id);

		sfProduct.setProjectId(id);

		int total = sfProductService.elementCount(id); // 配件总数

		int totalPages = PageDemoUtil.Comparison(total, everyPage); // 总页数

		PageDemo pd = new PageDemo();
		pd.setValueId(id);
		pd.setBeginIndex(0);
		pd.setEndinIndex(4);
		int current = 1;

		if (sfProduct != null) {

			List<SfAttachment> SA = sfProductService.findProductPic(sfProduct
					.getId());

			for (SfAttachment sfAttachment : SA) {

				if (sfAttachment != null) {
					// 把SfAttachment对象集合的第一个对象的url属性添加到产品的图像属性上
					sfProduct.setProductPicURL(SA.get(0).getUrl());
				}

			}
		}

		List<SfElement> list = sfProductService.findElement(pd);
		if(!list.isEmpty()){
			
		
		
		for (SfElement Element : list) {
			if (Element != null) {
				List<SfAttachment> SA = sfElementService.findElementPic(Element
						.getId());
				for (SfAttachment sfAttachment : SA) {
					if (sfAttachment != null) {

						Element.setElemenetPicURL(SA.get(0).getUrl());

					}

				}

			} else {
				return "modules/project/eninterface/productUtil";
				
				
			}
		}
		
		}else{
			
			return "modules/project/eninterface/productUtil";
		}

		model.addAttribute("sfProduct", sfProduct);

		model.addAttribute("list", list);

		model.addAttribute("current", current);

		model.addAttribute("tp", totalPages);

		model.addAttribute("pid", pid);
		
		model.addAttribute("total", total);

		return "modules/project/eninterface/productForm";
	}
	
	
	/** 英文版 界   **/
}
